import java.util.Arrays;

public class MyArrayList implements IList{

    //存储数据
    private int[] elem;

    //有效数据
    private int usedSize;

    public MyArrayList() {
        this.elem = new int[Constant.DEFAULT_CAPACITY];
    }

    @Override
    public void add(int data) {
        //判断顺序表是否满了
        if(isFull()){
            //扩容
            grow();
        }

        //存入数据
        this.elem[this.usedSize] = data;
        //记录有效数据
        this.usedSize++;
    }

    public boolean isFull(){
        //判断
        return this.usedSize == elem.length;
    }

    private void grow(){
        //扩容
        this.elem = Arrays.copyOf(this.elem,2*elem.length);
    }

    @Override
    public void add(int pos, int data) {
        //判断是不是满的
        if(isFull()){
            grow();
        }
        String msg = Constant.ADD_POS_ILLEGALITY+pos;
        checkPosAdd(pos,msg);
        //判断是不是放在usedSize位置
        if (pos == usedSize){
            elem[pos] = data;
            usedSize++;
            return;
        }
        //移动数据
        for (int i = usedSize-1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }

    public void checkPosAdd(int pos,String msg){
        if(pos < 0 || pos > usedSize) {
            throw new PosIllegalityException(msg);
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        if (isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        String msg = Constant.GET_POS_ILLEGALITY+pos;
        checkPos(pos,msg);
        return elem[pos];
    }

    public boolean isEmpty(){
        return usedSize==0;
    }

    private void checkPos(int pos,String msg){
        if(pos < 0 || pos >=usedSize){
            throw new PosIllegalityException(msg);
        }
    }

    @Override
    public void set(int pos, int value) {
        if (isEmpty()){
            throw new EmptyListException(Constant.EMPTY_LIST);
        }
        String msg = Constant.SET_POS_ILLEGALITY+pos;
        checkPos(pos,msg);
        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for (int i = 0; i <this.usedSize ; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
}
